<?php

use console\db\Migration;

/**
 * VIP相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091356_t_vip extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * VIP类型表
         * @table `vip_type`
         */
        $this->tableBuilder([
            'name' => 'vip_type',
            'comment' => 'VIP类型表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('类型名称'),
                'thumb' => $this->char(255)->comment('类型图片'),
                'description' => $this->string(255)->defaultValue('')->comment('类型介绍'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('启用状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
        ]);

        /**
         * VIP充值价格表
         * @table `vip_type_price`
         */
        $this->tableBuilder([
            'name' => 'vip_type_price',
            'comment' => 'VIP充值价格表',
            'columns' => [
                'id' => $this->primaryKey(),
                'type_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('vip类型'),
                'name' => $this->string(255)->notNull()->comment('价位名称'),
                'price' => $this->decimal(6, 2)->unsigned()->notNull()->defaultValue(0)->comment('单价'),
                'discount' => $this->decimal(6, 2)->unsigned()->notNull()->defaultValue(0)->comment('折扣'),
                'duration' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('时长'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('启用状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
        ]);

        /**
         * VIP特权信息表
         * @table `vip_privilege`
         */
        $this->tableBuilder([
            'name' => 'vip_privilege',
            'comment' => 'VIP特权信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('会员特权名称'),
                'type_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('vip类型'),
                'thumb' => $this->char(255)->comment('特权图片'),
                'description' => $this->string(255)->defaultValue('')->comment('会员特权介绍'),
                'model' => $this->char(50)->notNull()->comment('应用范围'),
                'model_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('应用范围ID'),
                'begin_time' => $this->unixTimestamp()->comment('特权生效时间'),
                'end_time' => $this->unixTimestamp()->comment('特权过期时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);

        /**
         * VIP等级信息表
         * @table `vip_level`
         */
        $this->tableBuilder([
            'name' => 'vip_level',
            'comment' => 'VIP等级信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('等级名称'),
                'icon' => $this->char(255)->notNull()->comment('等级图标'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);


        /**
         * VIP充值订单表
         * @table `vip_order`.
         */
        $this->tableBuilder([
            'name' => 'vip_order',
            'comment' => 'VIP充值订单表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'type_id' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('VIP会员类型'),
                'nums' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('购买类型数量'),
                'time' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('时间长度'),
                'trade_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('支付订单ID'),
                'trade_no' => $this->char(32)->defaultValue('')->comment('交易订单号'),
                'pay_time' => $this->unixTimestamp()->comment('支付时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('订单状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);


        /**
         * 用户VIP身份表
         * @table `user_vip`.
         */
        $this->tableBuilder([
            'name' => 'user_vip',
            'comment' => '用户VIP身份表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'type_id' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('VIP会员类型'),
                'level_id' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('VIP等级'),
                'expired_at' => $this->unixTimestamp()->comment('VIP过期时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('VIP状态'),
                'created_at' => $this->unixTimestamp()->comment('首次成为VIP会员时间'),
                'updated_at' => $this->unixTimestamp()->comment('最后更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                    'unique' => true,
                ]
            ]
        ]);
        
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%user_vip}}');
        $this->dropTable('{{%vip_order}}');
        $this->dropTable('{{%vip_level}}');
        $this->dropTable('{{%vip_privilege}}');
        $this->dropTable('{{%vip_type_price}}');
        $this->dropTable('{{%vip_type}}');
    }

}
